﻿'Finished 3.5.0 Version
'Oct 19, 2009
'Deep Sky Labs

Imports System.Windows.Forms
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Threading

Public Class Window1

    Public ActiveControl As String
    Public Operators() As String = {"Addition", "Diference", "Multiplicate", "Divide"}

    Delegate Sub bg_image(ByVal Source As String)
    Public Sub SetBgImage(ByVal Source As String)
        Try
            Me.Image1.Source = WPFFunctions.ConvertImage(Source, 1000, 1000)
        Catch ex As Exception
        End Try
    End Sub

    Delegate Sub ScriptExecuter()
    Public Sub RunScript()
        Try
            Dim dlg As New Dialogs.SecurityDialog
            dlg.CurrentScript = ScriptFileName.FullName
            System.Console.WriteLine(ScriptFileName.FullName)

            Dim [Continue] As Boolean = False

            Dim DontShow As Boolean = _
                Boolean.Parse(GetSetting("OnFly Calculator", "ScriptSecurityDialogs", ScriptFileName.FullName, "False"))

            If DontShow = True Then : [Continue] = True
            Else
                If dlg.ShowDialog = Forms.DialogResult.OK Then
                    [Continue] = True
                End If
            End If

            If [Continue] = True Then

                Dispatcher.BeginInvoke(New BussyMessUpdater(AddressOf UpdateBussyMess), _
                                  Windows.Threading.DispatcherPriority.Background, _
                                  "Executing script...")

                Cintie.CintieConsole.About()

                Dim CResult As New Object
                Dim Compiler As New Cintie.ScriptEngine.Compiler

                SaveSetting("OnFlyCalculator", "Information", "ScriptDir", ScriptDir.FullName)
                SaveSetting("OnFlyCalculator", "Information", "OFCDir", OFCDirectory)
                SaveSetting("OnFlyCalculator", "Information", "Vars.X", TextBox2.Text)
                SaveSetting("OnFlyCalculator", "Information", "Vars.Y", TextBox3.Text)
                Dim strResults As String = ""
                For Each Item As TextBlock In ListBox1.Items
                    strResults = strResults & Item.Text & ";"
                Next
                SaveSetting("OnFlyCalculator", "Information", "vars.Result", strResults)
                Try
                    CResult = Compiler.Compile(CurScript)
                Catch ex As Exception
                End Try

                'Try
                'For Each Str As Object In CResult.Result.SendToResult("15")
                'ListView1.Items.Add(Str)
                'Next
                'Catch ex1 As Exception
                'End Try
                Try
                    Dim tb As New TextBlock
                    tb.Text = CResult.MainF("0")
                    ListBox1.Items.Add(tb)
                Catch ex As Exception
                    My.Windows.Console.ListBox1.Items.Add(ex.Message)
                    My.Windows.Console.ListBox1.Items.Add(ex.StackTrace)
                End Try
                Try
                    TextBox2.Text = CResult.SendToXVar("1")
                Catch ex2 As Exception
                    My.Windows.Console.ListBox1.Items.Add(ex2.Message)
                    My.Windows.Console.ListBox1.Items.Add(ex2.StackTrace)
                End Try

                Try
                    TextBox3.Text = CResult.SendToYVar("2")
                Catch ex3 As Exception
                    My.Windows.Console.ListBox1.Items.Add(ex3.Message)
                    My.Windows.Console.ListBox1.Items.Add(ex3.StackTrace)
                End Try
                Try
                    ComboBox1.Text = CResult.SendToOperator("3")
                Catch ex4 As Exception
                    My.Windows.Console.ListBox1.Items.Add(ex4.Message)
                    My.Windows.Console.ListBox1.Items.Add(ex4.StackTrace)
                End Try
                Try
                    'My.Computer.FileSystem.CopyDirectory(CResult.Result.InstallSkin("4"), My.Application.Info.DirectoryPath & "\Skins")
                    Dim Dr As IO.DirectoryInfo = Nothing
                    Dr = New IO.DirectoryInfo(CResult.Result.InstallSkin("4"))
                    Dim Outdir As String = OFCDirectory & _
                        "\Skins\" & Dr.Name
                    My.Computer.FileSystem.CreateDirectory(Outdir)
                    My.Computer.FileSystem.CopyDirectory(Dr.FullName, Outdir)
                Catch ex5 As Exception
                    'MsgBox(ex.Message)
                End Try
            Else
                MsgBox("OnFly Calculator have not permission to run this script")
            End If
            Dim _title As String = ""
            If CurScript.Information.Title = "" Then
                _title = ScriptFileName.Name
            Else
                _title = CurScript.Information.Title
            End If
            Dispatcher.BeginInvoke(New BussyMessUpdater(AddressOf UpdateBussyMess), _
                                   Windows.Threading.DispatcherPriority.Background, _
                                   String.Format("Editing: {0}", _title))
        Catch ex As Exception
        End Try
    End Sub

    Delegate Sub ProjectLoader(ByVal Prject As IO.FileInfo)


    Private Sub _LoadProject(ByVal Project As IO.FileInfo)
        Dispatcher.BeginInvoke(New BussyMessUpdater(AddressOf UpdateBussyMess), _
                               Windows.Threading.DispatcherPriority.Background, _
                               String.Format("Loading {0}", Project.Name))
        LoadProject(Project)
        TextBox1.Text = CurScript.Code
        Dim _title As String = ""
        If CurScript.Information.Title = "" Then
            _title = Project.Name
        Else
            _title = CurScript.Information.Title
        End If
        Dispatcher.BeginInvoke(New BussyMessUpdater(AddressOf UpdateBussyMess), _
                               Windows.Threading.DispatcherPriority.Background, _
                               String.Format("Editing: {0}", _title))
    End Sub

    Delegate Sub BussyMessUpdater(ByVal Text As String)
    Public Sub UpdateBussyMess(ByVal text As String)
        Label1.Content = text
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Try
            With ComboBox1
                If ComboBox1.SelectedIndex > (Operators.Length - 1) Then
                    mnuEjecutar_Click(Nothing, Nothing)
                Else
                    Dim X As Single, Y As Single, R As Single
                    If TextBox2.Text = "" Then : TextBox2.Text = "0"
                    End If
                    If TextBox3.Text = "" Then : TextBox3.Text = "0"
                    End If
                    X = TextBox2.Text
                    Y = TextBox3.Text
                    Dim tb As New TextBlock
                    Select Case Operators(ComboBox1.SelectedIndex)
                        Case "Addition"
                            R = X + Y
                            tb.Text = X & " + " & Y & " = " & R
                            ListBox1.Items.Add(tb)
                        Case "Diference"
                            R = X - Y
                            tb.Text = X & " - " & Y & " = " & R
                            ListBox1.Items.Add(tb)
                        Case "Multiplicate"
                            R = X * Y
                            tb.Text = X & " * " & Y & " = " & R
                            ListBox1.Items.Add(tb)
                        Case "Divide"
                            R = X / Y
                            tb.Text = X & " / " & Y & " = " & R
                            ListBox1.Items.Add(tb)
                    End Select
                End If

            End With

        Catch ex As Exception
            Dim tb As New TextBlock
            tb.Text = ex.Message
            ListBox1.Items.Add(tb)
        End Try


    End Sub
    Private Sub mnuBackGround_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles mnuBackGround.Click
        Dim dlg As New Window2
        dlg.ParentControl = "MainForm_CodeBox_BackGround"
        dlg.Show()

    End Sub
    Private Sub mnuOpen_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles mnuOpen.Click
        Dim Open As New OpenFileDialog
        Open.ShowDialog()
        Try
            Select Case MsgBox("Unsaved data will be lost, do you want continue opening a project?", MsgBoxStyle.YesNo)
                Case MsgBoxResult.Yes
                    TextBox1.Text = My.Computer.FileSystem.ReadAllText(Open.FileName)
                Case MsgBoxResult.No
                    Exit Sub
            End Select
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub mnuSave_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles mnuSave.Click
        SaveProject()
    End Sub

    Private Sub mnuOpenProject_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles mnuOpenProject.Click
        Dim Open As New OpenFileDialog
        Open.Filter = "OnFly Calculator Script (*.ofcs)|*.ofcs"
        If Open.ShowDialog = Forms.DialogResult.OK Then
            Try
                Dim file As New IO.FileInfo(Open.FileName)
                _LoadProject(file)
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub mnuSaveProject_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles mnuSaveProject.Click
        
    End Sub

    Private Sub MenuRefs_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles MenuRefs.Click
        Try
            Dim RefsM As New ReferencesManager
            For Each Ob As Object In CurScript.ReferencesList
                RefsM.ListBox1.Items.Add(Ob)
            Next
            RefsM.Show()
        Catch ex As Exception
        End Try
    End Sub


    Private Sub mnuEjecutar_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles mnuEjecutar.Click
        Try
            Dispatcher.BeginInvoke(New ScriptExecuter(AddressOf RunScript), _
                                   Windows.Threading.DispatcherPriority.Background, _
                                   Nothing)
        Catch ex As Exception
        End Try
    End Sub

    Private Sub mnuConsole_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles mnuConsole.Click
        Dim Console As New Console
        Console.Show()
        'For Each Ob As Object In ScriptsCore.Errors
        'Console.ListBox1.Items.Add(Ob.ToString)
        'Next
    End Sub

    Private Sub mnuForeGround_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles mnuForeGround.Click
        Dim dlg As New Window2
        dlg.ParentControl = "MainForm_CodeBox_ForeGround"
        dlg.Show()
    End Sub

    Private Sub FontPropertys_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles FontPropertys.Click
        Dim fntDialog As New FontDialog
        fntDialog.ShowDialog()
        Dim fntString As String = fntDialog.Font.Name
        If fntDialog.Font.Bold = True Then
            fntString &= " " & "Bold"
        End If
        If fntDialog.Font.Italic = True Then
            fntString &= " " & "Italic"
        End If
        MsgBox(fntString)
        Dim fnt As New System.Windows.Media.FontFamily(fntString)
        TextBox1.FontFamily = fnt
        TextBox1.FontSize = fntDialog.Font.Size
    End Sub

    Private Sub mnuSkin_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles mnuSkin.Click
        Dim sk As New Window5
        sk.Show()
    End Sub

    Private Sub Slider1_ValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of System.Double)) Handles Slider1.ValueChanged
        Dim op As Double = e.NewValue * 0.01
        TextBox1.Opacity = op
        TextBox2.Opacity = op
        TextBox3.Opacity = op
        Try
            ListBox1.Opacity = op
        Catch ex As Exception
        End Try
        Menu1.Opacity = op
        Button1.Opacity = op
        ComboBox1.Opacity = op
        Label1.Opacity = op
        Label2.Opacity = op

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
        Select Case ActiveControl
            Case "1"
                TextBox2.Text &= 7
            Case "2"
                TextBox3.Text &= 7
        End Select
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button3.Click
        Select Case ActiveControl
            Case "1"
                TextBox2.Text &= 8
            Case "2"
                TextBox3.Text &= 8
        End Select
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button4.Click
        Select Case ActiveControl
            Case "1"
                TextBox2.Text &= 9
            Case "2"
                TextBox3.Text &= 9
        End Select
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button5.Click
        Select Case ActiveControl
            Case "1"
                TextBox2.Text &= 4
            Case "2"
                TextBox3.Text &= 4
        End Select
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button6.Click
        Select Case ActiveControl
            Case "1"
                TextBox2.Text &= 5
            Case "2"
                TextBox3.Text &= 5
        End Select
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button7.Click
        Select Case ActiveControl
            Case "1"
                TextBox2.Text &= 6
            Case "2"
                TextBox3.Text &= 6
        End Select
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button8.Click
        Select Case ActiveControl
            Case "1"
                TextBox2.Text &= 1
            Case "2"
                TextBox3.Text &= 1
        End Select
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button9.Click
        Select Case ActiveControl
            Case "1"
                TextBox2.Text &= 2
            Case "2"
                TextBox3.Text &= 2
        End Select
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button10.Click
        Select Case ActiveControl
            Case "1"
                TextBox2.Text &= 3
            Case "2"
                TextBox3.Text &= 3
        End Select
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button12.Click
        Select Case ActiveControl
            Case "1"
                TextBox2.Text &= 0
            Case "2"
                TextBox3.Text &= 0
        End Select
    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button14.Click
        Select Case ActiveControl
            Case "1"
                TextBox2.Text &= "."
            Case "2"
                TextBox3.Text &= "."
        End Select
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button11.Click
        Select Case ActiveControl
            Case "1"
                Dim I As Integer = Len(TextBox2.Text) - 1
                Dim I2 As Integer = 0
                Dim X As String = ""
                While I2 < I
                    X = X & TextBox2.Text.Chars(I2)
                    I2 = I2 + 1
                End While
                TextBox2.Text = X
            Case "2"
                Dim I As Integer = Len(TextBox3.Text) - 1
                Dim I2 As Integer = 0
                Dim X As String = ""
                While I2 < I
                    X = X & TextBox3.Text.Chars(I2)
                    I2 = I2 + 1
                End While
                TextBox3.Text = X
        End Select
    End Sub

    Private Sub TextBox2_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles TextBox2.MouseDoubleClick
        ActiveControl = "1"
    End Sub

    Private Sub TextBox3_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles TextBox3.MouseDoubleClick
        ActiveControl = "2"
    End Sub

    Private Sub cmnuSumar_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmnuSumar.Click
        ComboBox1.Text = "Addition"
        Button1_Click(Nothing, Nothing)
    End Sub

    Private Sub cmnuRestar_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmnuRestar.Click
        ComboBox1.Text = "Diference"
        Button1_Click(Nothing, Nothing)
    End Sub

    Private Sub cmnuMultiplicar_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmnuMultiplicar.Click
        ComboBox1.Text = "Multiplicate"
        Button1_Click(Nothing, Nothing)
    End Sub

    Private Sub cmnuDividir_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmnuDividir.Click
        ComboBox1.Text = "Divide"
        Button1_Click(Nothing, Nothing)
    End Sub

    Private Sub Window1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        My.Application.Shutdown()
    End Sub

    Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        'Create required directories
        Dim Dir As New IO.DirectoryInfo(OFCDirectory & "\Scripts")
        If Dir.Exists = False Then
            Dir.Create()
        End If
        Dim SkinsDir As New IO.DirectoryInfo(OFCDirectory & "\Skins")
        If SkinsDir.Exists = False Then
            SkinsDir.Create()
        End If

        Dim frun As String = GetSetting("OnFlyCalculator", "frun", "3.5.0", "True") ' check if first run
        If Boolean.Parse(frun) = True Then
            Try
                Dim SkinsipDir As New IO.DirectoryInfo(AppPath & "\Skins")
                If SkinsipDir.Exists = False Then
                    SkinsipDir.Create()
                End If
                Dim ScriptsipDir As New IO.DirectoryInfo(AppPath & "\Scripts")
                If ScriptsipDir.Exists = False Then
                    ScriptsipDir.Create()
                End If
                For Each file As IO.FileInfo In ScriptsipDir.GetFiles("*.ofcs")
                    Try
                        file.CopyTo(OFCDirectory & "\Scripts\" & file.Name)
                        Dim ipDir As New IO.DirectoryInfo(file.FullName & ".storage")
                        If ipDir.Exists = True Then
                            My.Computer.FileSystem.CopyDirectory(ipDir.FullName, OFCDirectory & "\Scripts\" & ipDir.Name)
                        End If
                    Catch ex As Exception
                    End Try

                Next
                For Each file As IO.FileInfo In SkinsipDir.GetFiles("*.xaml")
                    Try
                        file.CopyTo(OFCDirectory & "\Skins\" & file.Name)
                        Dim ipDir As New IO.DirectoryInfo(file.FullName & ".storage")
                        If ipDir.Exists = True Then
                            My.Computer.FileSystem.CopyDirectory(ipDir.FullName, OFCDirectory & "\Skins\" & ipDir.Name)
                        End If
                    Catch ex As Exception
                    End Try
                Next
                SaveSetting("OnFlyCalculator", "frun", "3.5.0", "False")
            Catch ex As Exception

            End Try
        End If

        'fill scripts lists
        Dim Arr() As IO.FileInfo = Dir.GetFiles("*.ofcs") 'OnFly Calculator Script
        For Each f As IO.FileInfo In Arr
            ComboBox1.Items.Add(f.Name)
        Next

        'Get Command line args
        If Environment.GetCommandLineArgs.Length > 1 Then
            _LoadProject(New IO.FileInfo(Environment.GetCommandLineArgs(1)))
            Dispatcher.BeginInvoke(New ScriptExecuter(AddressOf RunScript), _
                                   Windows.Threading.DispatcherPriority.Background, _
                                   Nothing)
        End If

        'Load Skin
        Try
            Dim file As New IO.FileInfo(My.Settings.SkinFile)
            Dim rd As ResourceDictionary = Nothing
            Using fs As New IO.FileStream(file.FullName, IO.FileMode.Open, IO.FileAccess.Read)
                rd = CType(Windows.Markup.XamlReader.Load(fs), ResourceDictionary)
            End Using
            Application.Current.Resources = rd
        Catch ex As Exception

        End Try

    End Sub

    Private Sub mnuInstallScript_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles mnuInstallScript.Click
        Dim open As New System.Windows.Forms.OpenFileDialog
        open.Filter = "OnFly Calculator Script (*.ofcs)|*.ofcs"
        'open.ShowDialog()
        If open.ShowDialog = Forms.DialogResult.OK Then
            Dim ipFile As New IO.FileInfo(open.FileName)
            Dim ipDir As New IO.DirectoryInfo(open.FileName & ".storage")
            Try
                ipFile.CopyTo(OFCDirectory & "\Scripts\" & ipFile.Name)
                If ipDir.Exists = True Then
                    My.Computer.FileSystem.CopyDirectory(ipDir.FullName, OFCDirectory & "\Scripts\" & ipDir.Name)
                End If
                ComboBox1.Items.Add(ipFile.Name)
            Catch ex As Exception

            End Try

        End If
    End Sub

    Private Sub mnuviewscripts_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles mnuviewscripts.Click
        Dim dir As String = OFCDirectory & "\Scripts"
        Try
            System.Diagnostics.Process.Start(dir)
        Catch ex As Exception
        End Try
    End Sub

    Private Sub ComboBox1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ComboBox1.SelectionChanged
        If ComboBox1.SelectedIndex >= Operators.Count Then
            Try
                Dim proj As New IO.FileInfo(OFCDirectory & "\Scripts\" & ComboBox1.SelectedItem.ToString)
                _LoadProject(proj)
                'TextBox1.Text = CurScript.Code
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

    End Sub

    Private Sub mnuResources_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles mnuResources.Click
        If ScriptDir Is Nothing Then : Exit Sub
        End If
        Dim ResDir As IO.DirectoryInfo = ScriptDir
        If ResDir.Exists = False Then : ResDir.Create()
        End If
        System.Diagnostics.Process.Start(ResDir.ToString)
    End Sub

    Private Sub OPList_MnuCopy_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles OPList_MnuCopy.Click
        Try
            My.Computer.Clipboard.SetText(ListBox1.SelectedItem.Text)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub OPList_Paste_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles OPList_Paste.Click
        Try
            Dim tb As New TextBlock
            tb.Text = My.Computer.Clipboard.GetText
            ListBox1.Items.Add(tb)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub OPList_MnuCopyAll_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles OPList_MnuCopyAll.Click
        Dim str As String = ""
        For Each Ob As TextBlock In ListBox1.Items
            'MsgBox(Ob.ToString)
            str &= Ob.Text & vbCrLf
        Next
        Try
            My.Computer.Clipboard.SetText(str)
        Catch ex As Exception

        End Try

    End Sub


    Private Sub OPList_MnuClear_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        ListBox1.Items.Clear()
    End Sub

    Private Sub RadioButton1_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RadioButton1.Checked
        ActiveControl = 1
    End Sub

    Private Sub RadioButton2_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RadioButton2.Checked
        ActiveControl = 2
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles TextBox1.TextChanged
        Try
            CurScript.Code = TextBox1.Text
        Catch ex As Exception
        End Try
    End Sub

    Public Sub DownloadFile(ByVal UriString As String, ByVal Destination As String)
        Try
            Dim WebClient As New System.Net.WebClient
            Dim buff() As Byte = _
                WebClient.DownloadData(UriString)
            Dim Writer As New IO.StreamWriter(Destination)
            Writer.Write(System.Text.Encoding.UTF8.GetString(buff))
            Writer.Close()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub mnuSaveProjectAs_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim dlg As New SaveFileDialog
        dlg.Filter = "OnFly Calculator Script (*.ofcs)|*.ofcs"
        dlg.AddExtension = True
        If dlg.ShowDialog = Forms.DialogResult.OK Then
            If (ScriptDir.GetFiles.Length + ScriptDir.GetDirectories.Length = 0) Then
                SaveProject(dlg.FileName)
            Else
                SaveProject(dlg.FileName)
                Dim op As String = dlg.FileName & ".storage"
                Try
                    My.Computer.FileSystem.CopyDirectory(ScriptDir.FullName, op, True)
                Catch ex As Exception
                End Try

            End If
        End If

    End Sub

    Private Sub mnuNewProject_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim dlg As New NewProjectDialog
        dlg.ShowDialog()
        If dlg.DialogResult = Forms.DialogResult.OK Then
            LoadProject(New IO.FileInfo(dlg.FileName))
            TextBox1.Text = CurScript.Code
        End If
    End Sub

    Private Sub mnuHlp_FindHelp_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        System.Diagnostics.Process.Start("http://onflycalculator.deepskylabs.com/help/")
    End Sub

    Private Sub mnuHlp_AboutOFC_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        System.Diagnostics.Process.Start("http://onflycalculator.deepskylabs.com")
    End Sub

    Private Sub mnuHelp_AboutScript_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim dlg As New ScriptInfo
        'Try
        dlg.ShowInfo(CurScript.Information)
        'Catch ex As Exception
        'End Try
    End Sub

    Private Sub mnuHelp_HelpforScript_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            System.Diagnostics.Process.Start(CurScript.Information.HelpUri)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub TopMenu_Repos_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim dlg As New RepositoryManager
        If dlg.ShowDialog = Forms.DialogResult.OK Then
            Dim _f As New IO.FileInfo(dlg.InstalledScript)
            ComboBox1.Items.Add(_f.Name)
        End If
    End Sub

    Private Sub TopMenu_News_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim wnd As New Window3
        wnd.Show()
    End Sub

End Class
